package com.suse.jd_system_server.controller;


import com.suse.jd_system_server.entity.User;
import com.suse.jd_system_server.service.UserService_rp;
import com.suse.jd_system_server.service.impl.OrdersServiceImpl_rp;
import com.suse.jd_system_server.service.impl.UserServiceImpl_rp;
import com.suse.jd_system_server.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/PersonalOrders")
@CrossOrigin()
public class UserOrdersController {
    @Autowired
    OrdersServiceImpl_rp ordersService;
    @Autowired
    UserServiceImpl_rp userServiceImpl_rp;
    @GetMapping("/getOrdersListById/{user_id}")
    public Result getOrdersListById (@PathVariable(value = "user_id")Long user_id){
        return Result.buildSuccess(ordersService.getOrdersList(user_id));
    }

    @GetMapping("/getOrderById/{order_id}")
    public Result getOrderById (@PathVariable(value = "order_id")Long order_id){
        return Result.buildSuccess(ordersService.getOrderById(order_id));
    }

    @PostMapping("/searchList")
    public Result searchList(@RequestBody Map<String,Object> param){
        System.out.println(param);
        return Result.buildSuccess(ordersService.searchList(param));
    }
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User param){
//        System.out.println(param);
        return Result.buildSuccess(userServiceImpl_rp.updateUser(param));
    }
    @GetMapping("/selectUser/{user_id}")
    public Result selectUser (@PathVariable(value = "user_id")Long user_id){
        return Result.buildSuccess(userServiceImpl_rp.selectUser(user_id));
    }
    @PostMapping("/confPWD")
    public Result confPWD (@RequestBody Map<String,Object> param){
//        System.out.println(param);
        return Result.buildSuccess(userServiceImpl_rp.confPWD(param));
    }
    @PostMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody Map<String,Object> param){
        System.out.println(param);
        return Result.buildSuccess(ordersService.cancelOrder(param));
    }
    @PostMapping("/uploadUi")
    public String addUser(@RequestParam("files") MultipartFile file) throws IllegalStateException, IOException {
        System.out.println(file.getOriginalFilename() + "图片已传入!!");
        byte[] b = file.getBytes();
        String str = Base64.getEncoder().encodeToString(b);
//        System.out.println(str);
        return str;
    }
}
